home *** CD-ROM | disk | FTP | other *** search
/ MacHack 1996 / MacHack 1996.toast / Hacks / Hacks ’89 / gadlife / source (ugly) / util.c < prev   
Encoding:
C/C++ Source or Header  |  1989-06-14  |  7.2 KB  |  284 lines  |  [TEXT/KAHL]

  1. #include "main.h"
  2.  
  3. #define modsWord    3
  4. #define shiftBit        1
  5. #define alphaBit    2
  6. #define optionBit    4
  7. #define cmdBit        0x8000
  8.  
  9. windowAt( h, v )
  10.     int            h, v;
  11.     {    
  12.     WindowPeek    theWindow;
  13.     
  14.     theWindow = (WindowPeek)FrontWindow();
  15.     while( theWindow )
  16.         {
  17.         if(( h == - theWindow->port.portBits.bounds.left ) &&
  18.            ( v == - theWindow->port.portBits.bounds.top ))
  19.             return( -1 );
  20.         theWindow = theWindow->nextWindow;
  21.         }
  22.     return( 0 );
  23.     }
  24.  
  25. newCorner( h, v )
  26.     int        *h, *v;
  27. {
  28.     Point        old;
  29.     int        sequence = 0, height;
  30.  
  31.     height = getMBarHeight();
  32.     old.h = 16;
  33.     old.v = 33 + height;
  34.     while( windowAt( old.h, old.v )) {
  35.         old.h += 16;
  36.         old.v += 16;
  37.         if( !PtInRgn( old, GrayRgn )) {
  38.             sequence = ( sequence + 2 ) % 16;
  39.             old.h = 16 - sequence;
  40.             old.v = sequence + 33 + height;
  41.         }
  42.     }
  43.     *h = old.h;
  44.     *v = old.v;
  45. }
  46.  
  47. getDataFontInfo( theFont, which )
  48.     dataFontPtr    theFont;
  49.     int            which;
  50. {
  51.     GrafPort        tempPort;
  52.     GrafPtr        savePort;
  53.     FontInfo        theFontInfo;
  54.     char            theString[ pasStrLen ];
  55.     
  56.     GetIndString( theString, fontNames, which );
  57.     if( !*theString ) theFont->number = 0;
  58.     else GetFNum( theString, &theFont->number );
  59.     GetIndString( theString, fontSizeStrs, which );
  60.     theFont->size = readPasNum( theString );
  61.     theFont->size = max( theFont->size, minFontSize );
  62.  
  63.     GetPort( &savePort );
  64.     OpenPort( &tempPort );
  65.     TextFont( theFont->number );
  66.     TextSize( theFont->size );
  67.     TextFace( 0 );
  68.  
  69.     GetFontInfo( &theFontInfo );
  70.     theFont->topStart = theFontInfo.ascent + topMargin;
  71.     theFont->topSpace = theFont->topStart + theFontInfo.descent + theFontInfo.leading + topMargin + 2;
  72.  
  73.     GetIndString( theString, statStrings, genStr );
  74.     if( *theString ) theFont->genStart = StringWidth( theString );
  75.     else theFont->genStart = StringWidth( "\pGeneration: " );
  76.     theFont->genStart += leftMargin;
  77.     SetPort( savePort );
  78.     ClosePort( &tempPort );
  79. }
  80.  
  81. /*******************************************************
  82. *                                                                *
  83. *    For now, this routine simply sets the minimum to a default which keeps the    *
  84. *    generation routines and such happy, and sets the maximum to very large.  In    *
  85. *    future, it could check against GrayRgn bounding box for the maximum.        *
  86. *                                                                *
  87. *******************************************************/
  88.  
  89. setSizeRect( theRect, theWindow )
  90.     Rect            *theRect;
  91.     WindowPtr    theWindow;
  92. {
  93.     dataHandle    theDataHand;
  94.     
  95.     theRect->bottom = theRect->right = 32767;
  96.     theRect->left = 63;
  97.     theDataHand = ( dataHandle )GetWRefCon( theWindow );
  98.     theRect->top = 63 + ( **theDataHand ).theFont.topSpace;
  99. }
  100.  
  101. readPasNum( source )
  102.     char        *source;
  103.     {
  104.     int        length, result = 0;
  105.     
  106.     length = ( int )*source++;
  107.     if( length )
  108.         while( length-- && *source >= '0' && *source <= '9' )
  109.             result = result * 10 + *source++ - '0';
  110.     if( length > -1 ) result = 0;
  111.     return( result );
  112.     }
  113.  
  114. getModifiers() {
  115.     int            theKeys[ 8 ];
  116.     register int    mods, result = 0;
  117.     
  118.     GetKeys( theKeys );
  119.     mods = theKeys[ modsWord ];
  120.     if( mods & shiftBit ) result |= shiftKey;
  121.     if( mods & alphaBit ) result |= alphaLock;
  122.     if( mods & optionBit ) result |= optionKey;
  123.     if( mods & cmdBit ) result |= cmdKey;
  124.     return( result );
  125. }
  126.  
  127. long timer()
  128.     {
  129.     static long        myEvents = 0;
  130.     
  131.     return( myEvents++ );
  132.     }
  133.  
  134. setBigRgn( theRgn )
  135.     RgnHandle        theRgn;
  136. {
  137.     SetRectRgn( theRgn, -32768, -32768, 32767, 32767 );
  138. }
  139.  
  140. /*******************************************************
  141. *                                                                *
  142. *    CopyBig is an exact equivalent of CopyBits (and as such is capitalized).  It        *
  143. *    merely ensures that CopyBits is never called with more than maxCopyBits    *
  144. *    bytes of data to copy.  Currently it repeatedly calls with the original mask    *
  145. *    region.  It may be more efficient to repeatedly clip to smaller portions of the    *
  146. *    mask.  That is, intersect it with a rectangle that repeatedly gets lower.        *
  147. *                                                                *
  148. *******************************************************/
  149.  
  150. CopyBig( source, dest, sourceRect, destRect, mode, mask )
  151.     BitMap        *source, *dest;
  152.     Rect            *sourceRect, *destRect;
  153.     int            mode;
  154.     RgnHandle        mask;
  155.     {
  156.     int            remaining, increment, factor;
  157.     Rect            lsource, ldest;
  158.     
  159.     startMyCopy();
  160.     remaining = sourceRect->bottom - sourceRect->top;
  161.     factor = ( sourceRect->right - sourceRect->left + 7 ) >> 3;
  162.     if( factor == 0 ) factor = 1;
  163.     increment = maxCopyBits / factor;
  164.     lsource = *sourceRect;
  165.     ldest = *destRect;
  166.     while( remaining > increment )
  167.         {
  168.         lsource.bottom = lsource.top + increment;
  169.         ldest.bottom = ldest.top + increment;
  170.         CopyBits( source, dest, &lsource, &ldest, mode, mask );
  171.         lsource.top = lsource.bottom;
  172.         ldest.top = ldest.bottom;
  173.         remaining -= increment;
  174.         }
  175.     lsource.bottom = sourceRect->bottom;
  176.     ldest.bottom = destRect->bottom;
  177.     CopyBits( source, dest, &lsource, &ldest, mode, mask );
  178.     endMyCopy();
  179.     }
  180.  
  181. /*******************************************************
  182. *                                                                *
  183. *    This allocates a new pointer direcly using the system call, and sets the clear    *
  184. *    flag when it does so.                                                *
  185. *                                                                *
  186. *******************************************************/
  187.  
  188. char *NewPtrClear( size )
  189.     long    size;
  190.     {
  191.     char    *ret;
  192.  
  193.     asm {
  194.         move.l    size,d0
  195.         NewPtr    CLEAR
  196.         move.l    a0,ret
  197.         }
  198.     return( ret );
  199.     }
  200.     
  201. /*******************************************************
  202. *                                                                *
  203. *    PtrToPtr is much like HandToHand - it takes a ptr*, and replaces the given    *
  204. *    pointer to a new one with the same contents.                            *
  205. *                                                                *
  206. *******************************************************/
  207.  
  208. ptrToPtr( ptr )
  209.     char    **ptr;
  210. {
  211.     long    size;
  212.     char    *new;
  213.     
  214.     size = GetPtrSize( *ptr );
  215.     new = NewPtr( size );
  216.     BlockMove( *ptr, new, size );
  217.     *ptr = new;
  218. }
  219.  
  220. /*******************************************************
  221. *                                                                *
  222. *    RgnIsRect returns true if the given region exactly equals the given rectangle.    *
  223. *                                                                *
  224. *******************************************************/
  225.  
  226. rgnIsRect( theRgn, theRect )
  227.     RgnHandle        theRgn;
  228.     Rect            *theRect;
  229. {
  230.     RgnHandle        tempRgn;
  231.     int            result;
  232.     
  233.     tempRgn = NewRgn();
  234.     RectRgn( tempRgn, theRect );
  235.     result = EqualRgn( tempRgn, theRgn );
  236.     DisposeRgn( tempRgn );
  237.     return( result );
  238. }
  239.  
  240. /*******************************************************
  241. *                                                                *
  242. *    SetRectBits initializes the given BitMap to exactly contain the given rectangle.    *
  243. *    It will always set rowBytes to a multiple of 4.                            *
  244. *                                                                *
  245. *******************************************************/
  246.  
  247. setRectBits( theBits, theRect )
  248.     BitMap        *theBits;
  249.     Rect            *theRect;
  250. {
  251.     theBits->bounds = *theRect;
  252.     theBits->rowBytes = (( theRect->right - theRect->left + 31 ) >> 3 ) & longMask;
  253.     theBits->baseAddr = NewPtrClear( (long)( theRect->bottom - theRect->top ) * theBits->rowBytes );
  254. }
  255.  
  256. /*******************************************************
  257. *                                                                *
  258. *    displayNumPas takes the number ( a long ), and the address of the *last*        *
  259. *    character in the destination array.  It returns the address of the count.  It is    *
  260. *    used both to convert the free memory count for the about box and to convert    *
  261. *    the generation count used in the automata windows.                        *
  262. *                                                                *
  263. *******************************************************/
  264.  
  265. displayNumPas( num, end, result )
  266.     long    num;
  267.     char    *end;
  268.     char    **result;
  269. {    
  270.     int    digits;
  271.     char    *realend = end;
  272.     
  273.     if( num )
  274.         for( digits = 0; num; ++digits, num /= 10 ) {
  275.             if( digits && ( digits % 3 == 0 ))
  276.                 *end-- = ',';
  277.             *end-- = num % 10 + '0';
  278.         }
  279.     else *end-- = '0';
  280.     *end = realend - end;        /* calculate pascal length */
  281.     *result = end;
  282. }
  283.  
  284.